import { Callout, Card, Cards, Steps, Tabs } from "nextra/components";
import UniversalTabs from "@/components/UniversalTabs";
import { GithubSnippet, getSnippets } from "@/components/code";

export const OnFailureTs = {
  path: "src/examples/on-failure.ts",
};

export const OnFailurePy = {
  path: "examples/on_failure/worker.py",
};

export const OnFailureGo = {
  path: "examples/on-failure/main.go",
};

export const getStaticProps = ({}) =>
  getSnippets([OnFailureTs, OnFailurePy, OnFailureGo]);

# On Failure Step

The On Failure Step is a special step in Hatchet workflows that allows you to define a function to be executed in the event that any step in the main workflow fails. This feature enables you to handle errors, perform cleanup tasks, or trigger notifications in case of workflow failures.

## Defining an On Failure Step

You can define an On Failure Step on your Workflow using the Hatchet client libraries:

<UniversalTabs items={["Python", "Typescript", "Go"]}>
  <Tabs.Tab title="Python">
    <GithubSnippet src={OnFailurePy} target="OnFailure Step" />
    <Callout variant="warning">
      Note: Only one `on failure` step can be defined per workflow.
    </Callout>
  </Tabs.Tab>
  <Tabs.Tab title="Typescript">
    <GithubSnippet src={OnFailureTs} target="OnFailure Step" />
  </Tabs.Tab>
  <Tabs.Tab title="Go">
    <GithubSnippet src={OnFailureGo} target="OnFailure Step" />
  </Tabs.Tab>
</UniversalTabs>

In the above examples, the `on failure` step is defined separately from the main workflow steps. It will be executed only if any of the main workflow steps fail.

## Use Cases

Some common use cases for the On Failure Step include:

- Performing cleanup tasks after a workflow failure
- Sending notifications or alerts about the failure
- Logging additional information for debugging purposes
- Triggering a compensating action or a fallback workflow

By utilizing the On Failure Step, you can handle workflow failures gracefully and ensure that necessary actions are taken in case of errors.

### Example: Sending a Slack Notification on Failure

```python
@hatchet.on_failure_step()
def on_failure(self, context):
    slack_client.send_message(
        f"Workflow {context.workflow_run_id()} failed"
    )
```

In this example, the On Failure Step sends a Slack notification with details about the failed workflow, including the workflow run ID which can be used to find the run in the Hatchet Dashboard.

By leveraging the On Failure Step, you can enhance the reliability and observability of your workflows, ensuring that failures are handled appropriately and relevant stakeholders are notified.
